﻿#include "TrChartPeriodBar.h"
#include "TrChartPartButton.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>

TrChartPeriodBar::TrChartPeriodBar(QWidget *parent) : QWidget(parent)
{

    setFixedSize(260, g_iTopMenuFixedHight);
    setLayout(new QHBoxLayout);
    layout()->setMargin(0);
    layout()->setSpacing(3);

    m_mapKlinePeriod.insert({"Tick", TrKlinePeriod::KP_Tick});
    m_mapKlinePeriod.insert({"1分", TrKlinePeriod::KP_Minute1});
    m_mapKlinePeriod.insert({"5分", TrKlinePeriod::KP_Minute5});
    m_mapKlinePeriod.insert({"15分", TrKlinePeriod::KP_Minute15});
    m_mapKlinePeriod.insert({"1小时", TrKlinePeriod::KP_Minute60});
    m_mapKlinePeriod.insert({"天", TrKlinePeriod::KP_DAY});
    m_mapKlinePeriod.insert({"周", TrKlinePeriod::KP_Week});

    int i = 0;
    for (auto it = m_mapKlinePeriod.rbegin(); it != m_mapKlinePeriod.rend(); it++) {
        QPushButton   *pBtn = new QPushButton(it->first);
        pBtn->setFixedHeight(g_iTopMenuFixedHight);
        pBtn->setStyleSheet("QPushButton{background-color:#151924;border:none;}QPushButton::hover {color:#2962ff;}");
        m_mapPartButtons.insert({i, pBtn});
        layout()->addWidget(pBtn);
        connect(pBtn, SIGNAL(clicked()), this, SLOT(OnClick()));
        i++;
    }

    activeBtn(TrKlinePeriod::KP_DAY);

}

void TrChartPeriodBar::OnClick()
{
    QPushButton *pBtn = qobject_cast<QPushButton *>(sender());
    for (auto it = m_mapKlinePeriod.rbegin(); it != m_mapKlinePeriod.rend(); it++) {
        if(it->first == pBtn->text())
        {
            if (m_trPeriod != it->second)
            {
                m_trPeriod = it->second;
                activeBtn(it->second);
                emit OnChangePeriod(m_trPeriod);
            }
            break;
        }
    }

}

void TrChartPeriodBar::activeBtn(TrKlinePeriod period)
{
    for (auto it = m_mapPartButtons.begin(); it != m_mapPartButtons.end(); it++) {
        if(period != it->first)
        {
            it->second->setStyleSheet("QPushButton{background-color:#151924;border:none;}QPushButton::hover {color:#2962ff;}");
        } else {
            it->second->setStyleSheet("QPushButton{background-color:#151924;color:#2962ff;border:none;}QPushButton::hover {color:#2962ff;}");
        }
    }
}

